package com.song.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.song.dto.ExcelDictDTO;
import com.song.entity.Dict;
import com.song.listener.ExcelDictDTOListener;
import com.song.mapper.DictMapper;
import com.song.service.DictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author Song
 * @since 2025-01-03
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importData(InputStream inputStream) {
         //存入
        //redisTemplate.opsForValue().set("dictList",dictList,30, TimeUnit.DAYS);

       // redisTemplate.opsForValue().get("dictList");


        EasyExcel.read(inputStream,
                ExcelDictDTO.class,new ExcelDictDTOListener(baseMapper)).sheet().doRead();
        System.out.println("service import ok le !!");

    }

    @Override
    public List<Dict> listByParentId(Long parentId) {
       //根据上级id 查询list
        List<Dict> dictList = baseMapper.
                selectList(new QueryWrapper<Dict>().eq("parent_id", parentId));
        dictList.forEach(dict -> {
            //如果是子节点，则是非叶子节点
            boolean result = this.hasChildren(dict.getId());
            //设置
            dict.setHasChildren(result);
        });

        return dictList;
    }

    @Override
    public List listData() {
        //明天这里要从redis中查出来
        List<Dict> dictList = baseMapper.selectList(null);

        List<ExcelDictDTO> excelDictDTOList = new ArrayList<>(dictList.size());

        dictList.forEach(dict->{
            ExcelDictDTO eddo = new ExcelDictDTO();
            //使用拷贝方法
            BeanUtils.copyProperties(dict,eddo);
            // eddo.setName(dict.getName());  依次赋值
            excelDictDTOList.add(eddo);


        });

        return excelDictDTOList;
    }

    @Override
    public List<Dict> findByDictCode(String dictCode) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);//查询条件
        Dict dict = baseMapper.selectOne(wrapper);

        return this.listByParentId(dict.getId());
    }

    @Override
    public String getNameByParentDictCodeAndValue(String dictCode, Integer value) {
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_code",dictCode);//查询条件
        Dict parentDict = baseMapper.selectOne(wrapper);

        if(parentDict==null){
             return "";
        }
        wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",parentDict.getId()).eq("value",value);

        Dict dict = baseMapper.selectOne(wrapper);

        if(dict==null){
            return "";
        }

        return dict.getName();
    }

    //判断当前节点是否存在子节点
    private boolean hasChildren(Long id){
        QueryWrapper<Dict> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);

        Integer count = baseMapper.selectCount(wrapper);

        if(count.intValue()>0){
            //存在子节点
            return true;
        }

       return false;
    }
}
